#!/usr/bin/python2.4
#
# Copyright 2010 Google Inc.

# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0

# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Cacluates Euclidean distance between solutions.

Calculates the shortest distance from a solution to a set of solutions.
The distance is calculated as the euclidean distance in the objective
space.
"""
__author__ = 'ntrolls@gmail.com (Shin Yoo)'

import math


class Distance(object):
  """Provides distance measurement methods."""

  def DistanceToSolutionSet(self, solution, solutionset):
    distance = 9999999999999.0
    for other in solutionset:
      aux = self.DistanceBetweenSolutionsInObjectSpace(solution, other)
      if aux < distance:
        distance = aux
    return distance

  def DistanceBetweenSolutionsInObjectSpace(self, a, b):
    diff = 0
    distance = 0
    for i in range(len(a.objectives)):
      diff = a.objectives[i] - b.objectives[i]
      distance += math.pow(diff, 2.0)
    return math.sqrt(distance)
